//
//  HMNaviViewController.swift
//  MicroBlog
//
//  Created by Apple on 16/9/19.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

class HMNaviViewController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        //设置边缘手势的代理
        self.interactivePopGestureRecognizer?.delegate = self
    }
    //重写Push方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        //如果子视图个数大于0说明不是根控制器
        if childViewControllers.count > 0 {
            if childViewControllers.count == 1 {
                //获取上一级的tilte
                let title = viewControllers.first?.title
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: title!, imageName: "navigationbar_back_withtext", target: self, action: #selector(backButtonClick))
            }else{
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(backButtonClick))
            }
            viewController.title = "当前显示第\(viewControllers.count + 1)级控制器"
            viewController.hidesBottomBarWhenPushed = true
        }
        super.pushViewController(viewController, animated: true)
        
    }
    
    func backButtonClick() {
        popViewController(animated: true)
    }
    
    //处理边缘手势
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        //根视图控制器不处理
        if viewControllers.count == 1{
            return false
        }
        return true
    }
}
